package cn.marinda.resturants.controller;

import cn.marinda.resturants.entity.Staff;
import cn.marinda.resturants.entity.Supplier;
import cn.marinda.resturants.enums.EntityJson;
import cn.marinda.resturants.global.Pages;
import cn.marinda.resturants.service.SupplierService;
import cn.marinda.resturants.utils.ApiResult;
import cn.marinda.resturants.utils.IOUtils;
import cn.marinda.resturants.utils.PagesResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Resource
    private SupplierService supplierService;

    /**
     * 返回所有供应商
     *
     * @return 供应商
     */
    @RequestMapping("/queryList")
    public ApiResult queryList() {
        List<Supplier> supplierList = supplierService.queryList();
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,supplierList);
    }

    /**
     * 根据菜品的uid来找到对应的供应商
     *
     * @param uid uid
     * @return 供应商
     * @author Marinda
     */
    @RequestMapping("/mealUid")
    public ApiResult selectMealUid(String uid) {
        Supplier supplier = supplierService.selectMealUid(uid);
        if(supplier == null){
            return new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);
        }
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,supplier);

    }

    /**
     * 模糊查询供货商姓名来找到这个供货商
     *
     * @param name 姓名
     * @return 供货商
     */
    @RequestMapping("/likeName")
    public ApiResult selectLikeName(String name) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,supplierService.selectLikeName(name));
    }

    @RequestMapping("/index")
    public ApiResult selectUid(String uid) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE, Arrays.asList(supplierService.selectUid(uid)));
    }

    /**
     * 查询指定供应商的单价
     *
     * @param uid uid
     * @return 价格单价
     */
    @RequestMapping("/unitPrice")
    public ApiResult selectUnitPrice(String uid) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,supplierService.selectUnitPrice(uid));
    }

    /**
     * 查询指定供应商的总价
     *
     * @param uid uid
     * @return 价格单价
     */
    @RequestMapping("/totalPrice")
    public ApiResult selectTotalPrice(String uid) {

        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,supplierService.selectTotalPrice(uid));
    }

    /**
     * 查询指定范围内的所有单价区间供应商
     *
     * @param startUnitPrice 开始单价
     * @param endUnitPrice   结束单价
     * @return 价格单价
     */
    @RequestMapping("/unitPriceInterval")
    public ApiResult selectUnitPriceInterval(BigDecimal startUnitPrice, BigDecimal endUnitPrice) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,supplierService.selectUnitPriceInterval(startUnitPrice, endUnitPrice));
    }

    /**
     * 查询指定范围内的所有价格区间供应商
     *
     * @param startTotalPrice 开始单价
     * @param endTotalPrice   结束单价
     * @return 指定范围内供应商
     */
    @RequestMapping("/totalPriceInterval")
    public ApiResult selectTotalPriceInterval(BigDecimal startTotalPrice, BigDecimal endTotalPrice) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,supplierService.selectTotalPriceInterval(startTotalPrice, endTotalPrice));
    }

    /**
     * 查询指定日期内上次进货时间区间
     *
     * @param startDateFormat 开始日期区间
     * @param endDateFormat   结束日期区间
     * @return 指定范围内供应商
     */
    @RequestMapping("/upDateInterval")
    public ApiResult selectUpDateInterval(String startDateFormat, String endDateFormat) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,supplierService.selectUpDateInterval(startDateFormat, endDateFormat));
    }

    /**
     * 查询指定日期内最新进货时间区间
     *
     * @param startDateFormat 开始日期区间
     * @param endDateFormat   结束日期区间
     * @return 指定范围内供应商
     */
    @RequestMapping("/newDateInterval")
    public ApiResult selectNewDateInterval(String startDateFormat, String endDateFormat) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,supplierService.selectNewDateInterval(startDateFormat, endDateFormat));
    }

    /**
     * 查询指定上次进货日期的供应商
     *
     * @param dateFormat 日期
     * @return 指定范围内供应商
     */
    @RequestMapping("/upDateUid")
    public ApiResult selectUpDateById(String dateFormat) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,supplierService.selectUpDateById(dateFormat));
    }

    /**
     * 查询指定最新进货日期的供应商
     *
     * @param dateFormat 日期
     * @return 指定范围内供应商
     */
    @RequestMapping("/newDateUid")
    public ApiResult selectNewDateById(String dateFormat) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,supplierService.selectNewDateById(dateFormat));
    }

    /**
     * 添加一名供应商
     *
     * @param supplier 供应商
     * @return 结果
     */
    @RequestMapping("/add")
    public ApiResult addSupplier(Supplier supplier) {
        return supplierService.addSupplier(supplier) ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) : new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);
    }


    /**
     * 删除一名供应商
     *
     * @param uid 供应商uid
     * @return 结果
     */
    @RequestMapping("/delete")
    public ApiResult removeSupplier(String uid) {
        return supplierService.removeSupplier(uid) ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) :  new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);
    }

    /**
     * 乐观锁修改供应商
     *
     * @param supplier 供应商
     * @return 结果
     */
    @RequestMapping("/update")
    public ApiResult updateSupplier(Supplier supplier) {
        return supplierService.updateSupplier(supplier) ? new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,null) : new ApiResult(ApiResult.ON_FAIL,ApiResult.FAIL_MESSAGE,null);
    }

    /**
     * 根据uid查询一名供应商
     *
     * @param uid 供应商uid
     * @return 供应商
     */
    @RequestMapping("/selectId")
    public ApiResult selectByUid(String uid) {
        return new ApiResult(ApiResult.ON_SUCCESS,ApiResult.SUCCESS_MESSAGE,supplierService.selectByUid(uid));
    }

    /** 列表分页
     *
     * @date 2021/12/1
     * @return 结果
     */
    @RequestMapping("/pages")
    private PagesResult selectPages(@RequestParam("page") int page, @RequestParam("limit") int limit)
    {
        IPage<Supplier> supplierIPage = supplierService.selectPages(new Pages<>(page,limit));
        return new PagesResult(0,supplierIPage.getRecords(),(int)supplierIPage.getTotal(),supplierIPage.getSize());
    }
}
